const md5 = require('md5')
const BaseController = require('./base')
const jwt = require('jsonwebtoken')

const HashSalt = ':Kaikeba@good!@123'
const createRule = {
  email:{type:"email"},
  nickname:{type:"string"},
  password:{type:"string"},
  captcha:{type:"string"}
}

class UserController extends BaseController {
  async login(){
    const {ctx,app} = this
    const {email,password,captcha,emailcode} = ctx.request.body

    if(captcha.toUpperCase() !== ctx.session.captcha.toUpperCase()){
      return this.error("验证码错误")
    }
    if(emailcode != ctx.session.emailcode){
      return this.error("邮箱验证码错误")
    }
    const user = await ctx.model.User.findOne({
      email,
      password: md5(password+HashSalt)
    })

    if(!user){
      return this.error("用户名密码错误")
    }
    // 用户的信息加密成token，返回
    const token = jwt.sign({
      _id:user._id,
      email,
    },app.config.jwt.secret,{
      expiresIn:'24h'
    })
    this.success({token,email,nickname:user.nickname})

  }

  async register(){
    const {ctx} = this
    try{
      // 校验传递的参数
      ctx.validate(createRule)
    }catch(e){
      return this.error('参数校验失败',-1,e.errors)
    }

    const {email,password,captcha,nickname} = ctx.request.body
    console.log({email,password,captcha,nickname})

    if(captcha.toUpperCase() !== ctx.session.captcha.toUpperCase()){
      return this.error("验证码错误")
    }
    if(await this.checkEmail(email)){
      this.error('邮箱重复啦')
    }else{
      const ret = await ctx.model.User.create({
        email,
        nickname,
        password:md5(password+HashSalt)
      })

      if(ret._id){
        this.message('注册成功')
      }
    }

    // this.success({name:'kkb'})
    
  }

  async verify(){
    // 校验用户名是否已经存在


  }
  async checkEmail(email){
    const user = await this.ctx.model.User.findOne({email})
    return user
  }
  async info(){
    const { ctx } = this;
    // 需要从token里面读取用户信息
    const {email} = ctx.state
    const user = await this.checkEmail(email)
    this.success(user)
  }
}

module.exports = UserController